import time
import socket
import threading


class TAcqPcs:
    
    r03 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x03,0x13,0x89,0x00,0x12])
    r04_2355 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x08,0xcf,0x00,0x1B])
    r04_4007 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x0f,0xa7,0x00,0x05])
    r04_7300 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1c,0x84,0x00,0x31])
    r04_7393 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1c,0xe1,0x00,0x1d])
    r04_8001 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1f,0x41,0x00,0x31])
    r04_8101 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1f,0xa5,0x00,0x31])
    cmdBufs = [r03,r04_2355, r04_7300 , r04_7393 ,r04_8001 , r04_8101]
    def __init__(self) -> None:
        self.host="hh.hhdata.cn"
        self.port = 31256
        #self.host = "192.168.100.231"
        #self.port = 502


        self.skt = None
        self.sendTime = 0.5

        
    def start(self):
        try:
            self.connect()
            connFlag = self.isConnected()
            if connFlag :
                while connFlag:
                    try:
                        for d in TAcqPcs.cmdBufs:
                            try: 
                                self.skt.send(d)
                                print('send:')
                                print(d)
                                t0 = time.time()
                                dlt = 0 
                                while dlt<3:
                                    time.sleep(2)
                                    rec = self.skt.recv(1024)
                                    recSize = len(rec)
                                    if recSize>0:
                                        print('rec:---')
                                        print(rec)
                                        break
                                    else:
                                        dlt = time.time() - t0
                                        print(dlt)
                            except Exception as erItem:
                                print(erItem)
                    except Exception as er :
                        print(er)
            else:
                print('connection is break;')
        except Exception as er:
            print(er)   
        
    def start__(self):
        try:
            self.connect()
            connFlag = self.isConnected()
            if connFlag :
                thSend = threading.Thread(target= self.thExe_Send)
                thSend.setDaemon(True)
                thSend.start()
                thRecv = threading.Thread(target= self.thExe_Recv)
                thRecv.setDaemon(True)
                #thRecv.start()
                while connFlag:
                    r = input('please input ...')
            else:
                print('connection is break;')
        except Exception as er:
            print(er)    
    def connect(self): 
        try:
            addr = (self.host , self.port)
            self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.skt.connect(addr) 
        except Exception as er: 
            print(er)    
    def isConnected(self):
        res = False
        try:
            res = self.skt!=None
            if res :
                res = not (self.skt._closed)
        except Exception as er:
            res = False
            print(er)    
        return res
    def thExe_Send(self):
        try:
            connFlag = self.isConnected()
            r03 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x03,0x13,0x89,0x00,0x12])
            r04_7201 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1c,0x21,0x00,0x08])
            r04_7300 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1c,0x84,0x00,0x31])
            r04_7393 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1c,0xe1,0x00,0x1d])
            r04_8001 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1f,0x41,0x00,0x31])
            r04_8101 = bytearray([0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x04,0x1f,0xa5,0x00,0x31])
            while connFlag :
                try:
                    buf = [r03,r04_7201, r04_7300 , r04_7393 ,r04_8001 , r04_8101]
                    for b in buf:
                        time.sleep(0.1)
                        self.skt.send(b)
                        print(b)
                except Exception as err:
                    print(err)
        except Exception as er:
            print(er)    
    def thExe_Recv(self):
        try:
            connFlag = self.isConnected() 
            while connFlag :
                try:
                    buf = self.skt.recv(1024)
                    bufSize = len(buf)
                    if bufSize>0:
                        print(buf)
                except Exception as err:
                    print(err)
        except Exception as er:
            print(er)    
        

if __name__ == "__main__":
    pcs = TAcqPcs()
    pcs.start()

    
